#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <hidapi.h>
#include <JoystickReader.h>
#include <QTimer>
#include "ThrumasterPusher.h"
#include "ThrumasterRocker.h"
#include "MyApplication.h"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    // JoystickReader joystickReader;
    ThrumasterPusher thrumasterPusher;
    ThrumasterRocker thrumasterRocker;
    ThrumasterRockerThread thrumasterRockerThread(&thrumasterRocker);
    ThrumasterPusherThread thrumasterPusherThread(&thrumasterPusher);
    thrumasterPusherThread.start();
    thrumasterRockerThread.start();

    MyApplication app(argc, argv, &thrumasterPusherThread, &thrumasterRockerThread);
    QQmlApplicationEngine engine;

    engine.rootContext()->setContextProperty("thrumasterPusher", &thrumasterPusher);
    engine.rootContext()->setContextProperty("thrumasterRocker", &thrumasterRocker);

    qmlRegisterType<ThrumasterPusher>("MyLib", 1, 0, "ThrumasterPusher");
    qmlRegisterType<ThrumasterRocker>("MyLib", 1, 0, "ThrumasterRocker");

    // engine.rootContext()->setContextProperty("joystickReader", &joystickReader);
    qmlRegisterType<JoystickReader>("MyLib", 1, 0, "JoystickReader");
    // QTimer timer;
    // timer.start(1);
    // QObject::connect(&timer, &QTimer::timeout, &joystickReader, &JoystickReader::readJoystickData);

    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
        &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
